<template>
  <div class="container">
    <div>
      <el-row :gutter="2">
        <el-col :span="5">
          <el-form ref="form" :model="form">
            <el-form-item label="选择月份：">
              <el-date-picker
                v-model="form.StartDate"
                type="month"
                placeholder="选择月份"
                value-format="yyyy-MM"
                style="width: 150px"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4">
          <el-form ref="form" :model="form" style="float: left">
            <el-button size="mini" type="primary" @click="getlistdata"
              >查 询</el-button
            >
            <el-button size="mini" type="success" @click="exportExcel"
              >导 出</el-button
            >
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table
      highlight-current-row
      v-loading="loading"
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
    >
      <el-table-column
        prop="车辆名称"
        label="车辆名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="所属基站"
        label="所属基站"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="行驶总里程"
        label="行驶总里程"
        align="center"
        :formatter="formatterDistance"
      ></el-table-column>
      <el-table-column
        prop="行驶总时长"
        label="行驶总时长"
        align="center"
        :formatter="formatterTime"
      ></el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import '@/assets/css/common.css'
import { formatDate } from '@/common/js/date.js'
import export2Excel from '@/utils/exportExcel/index.js'
export default {
  data () {
    return {
      currentPage: 1, //初始页
      pageSize: 20,    //    每页的数据
      loading: false,
      form: {
        StartDate: formatDate(new Date(), 'yyyy-MM'),
      },
      tableData: [],
      originalData: []
    }
  },
  created () {
    this.getlistdata()
  },
  methods: {
    formatterDistance (row, column, cellValue, index) {
      return cellValue + ' 公里'
    },
    formatterTime (row, column, cellValue, index) {
      return cellValue + ' 小时'
    },
    async getlistdata () {
      this.currentPage = 1;
      this.loading = true
      var url = 'RubbishVehicle/VehicleCheckInfoMonth?date=' + this.form.StartDate + '&ClientNo=' + localStorage.getItem('CLIENT_NO')
      const { data } = await this.$Http.get(url)
      // for(var i = 0; i < data.length; ) {
      //   data[i].timeHou = (data[i].出行总时长分钟 / 60).toFixed(1)
      //   data[i].timeStr = parseInt(data[i].出行总时长分钟 / 60) + '小时' + (data[i].出行总时长分钟 % 60)+ '分钟' 
      //   data[i].kilStr = (data[i].出行总里程米 / 1000).toFixed(1) + '公里'
      //   i++;  //只有在没有删除元素时才对索引 i++
      // }
      this.tableData = data;
      this.loading = false
    },
    // 导出Excel
    exportExcel () {
      let excelName = '车辆月行驶时长里程统计'
      const tHeader = ['车辆名称', '所属基站', '行驶总里程（公里）', '行驶总时长（小时）']
      const filterVal = ['车辆名称', '所属基站', '行驶总里程', '行驶总时长']
      export2Excel(tHeader, filterVal, this.tableData, excelName)
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      // this.getData();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize)
    }
  }
}
</script>
<style>
.content {
  overflow-y: scroll;
}
.el-col-6 {
  min-width: 400px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
</style>